<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right">
        <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
        <el-breadcrumb-item>分类管理</el-breadcrumb-item>
        <el-breadcrumb-item>分类列表</el-breadcrumb-item>
    </el-breadcrumb> 
    <el-card>
        <el-row>
            <el-col>
                <el-button type="primary" @click="addCate">添加分类</el-button>
            </el-col>
        </el-row>
        <!-- 分类区域 -->
        <tree-table border  :selection-type='false' :expand-type='false' :data='cateList' :columns='columns' show-index index-text='#' :show-row-hover="false">
            <!-- 是否有效 -->
            <template slot="isOk" slot-scope="scope">
                <i  style="color:lightgreen" class="el-icon-success" v-if="scope.row.cat_deleted===false "></i>
                <i class="el-icon-error" v-else style="color:red"></i>
            </template>  
             <!--排序  -->
             <template slot="order" slot-scope="scope">
                <el-tag size="mini" type="primary" v-if="scope.row.cat_level===0">一级</el-tag>
                <el-tag size="mini" type="warning" v-else-if="scope.row.cat_level===1">二级</el-tag>
                <el-tag size="mini" type="danger" v-else>三级</el-tag>
             </template>
             <!-- 操作 -->
            <template slot="opt">
                <el-button type="primary" icon="el-icon-edit" size="mini">编辑</el-button>
                <el-button type="warning" icon="el-icon-delete" size="mini">删除</el-button>
            </template>  
        </tree-table>
        <!-- 分页区 -->
        <el-pagination
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="queryInfo.pagenum"
            :page-sizes="[1, 2, 5, 10]"
            :page-size="queryInfo.pagesize"
            layout="total, sizes, prev, pager, next, jumper"
            :total="total">
        </el-pagination>
        <!-- 添加分类对话框 -->
        <el-dialog
            title="提示"
            :visible.sync="addCateDialogVisible"
            width="50%"
            @close='closeDialog'>
            <el-form ref="addCateFormRef" :model="addCateForm" :rules="addCateFormRules" label-width="80px">
                <el-form-item label="添加分类" prop="addname">
                    <el-input v-model="addCateForm.cat_name"></el-input>
                </el-form-item>
                <el-form-item label="父级分类" >
                    <el-cascader
                        expandTrigger='hover'
                        v-model="selectOption"
                        :options="parentCateList"
                        :props="options"
                        @change="parentCateListChange"
                        clearable 
                        change-on-select>
                    </el-cascader>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="addCateDialogVisible = false">取 消</el-button>
                <el-button type="primary" @click="addCateList">确 定</el-button>
            </span>
        </el-dialog>
    </el-card>
  </div>
</template>

<script>
export default {
    data(){
        return{
            queryInfo:{
                type:3,
                pagenum:1,
                pagesize:2
            },
            cateList:[],
            total:0,
            addCateDialogVisible:false,
            addCateForm:{
                cat_pid :0,
                cat_name:'',
                cat_level:0
            },
            options:
                {
                    value:'cat_id',
                    label:'cat_name',
                    children:'children'
                },
            parentCateList:[],
            addCateFormRules:{
                addname: [
                    { required: false, message: '请输入活动名称', trigger: 'blur' },
                    { min: 2, max: 5, message: '长度在 1 到 5 个字符', trigger: 'blur' }
                ]
            },
            selectOption:[],
            columns:[{
                        label:'分类名称',
                        prop:'cat_name'
                    },
                    {
                        label:'是否有效',
                        prop:'cat_deleted',
                        type:'template',
                        template:'isOk'
                    },
                    {
                        label:'排序',
                        prop:'cat_level',
                        type:'template',
                        template:'order'
                    },
                    {
                        label:'操作',
                        type:'template',
                        template:'opt'
                    }
                 ]
        }
    },
    created(){
        this.getGoosCateList()
    },
    methods:{
        async getGoosCateList(){
         const {data:res} = await this.$http.get('categories',{params:this.queryInfo})
         this.cateList = res.data.result
        //  console.log(res)
         this.total = res.data.total
        },
        handleSizeChange(newSize){
            this.queryInfo.pagesize = newSize
            this.getGoosCateList()
        },
        handleCurrentChange(newpage){
            this.queryInfo.pagenum = newpage
            this.getGoosCateList()
        },
        addCate(){
            this.getParentCateList()
            this.addCateDialogVisible = true
        },
        async getParentCateList(){
         const {data:res} = await  this.$http.get('categories',{params:{type:2}})
            console.log(res.data)
            this.parentCateList = res.data
        },
        parentCateListChange(){
            console.log(this.selectOption)
            if(this.selectOption.length>0){
                this.addCateForm.cat_pid=this.selectOption[this.selectOption.length-1]
                this.addCateForm.cat_level= this.selectOption.length
                return
            }else if(this.selectOption.length=0){
                this.addCateForm.cat_pid = 0
                this.addCateForm.cat_level = 0
            }
        },
        addCateList(){
            this.$refs.addCateFormRef.validate(async valid=>{
                if(!valid) return
                
                const {data:res}= await  this.$http.post('categories',this.addCateForm)
                console.log(res.data)
                // this.cateList = res.split('')
                this.getGoosCateList()
                this.addCateDialogVisible = false
            })
            
            // console.log(this.addCateForm)
        },
        closeDialog(){
            this.$refs.addCateFormRef.resetFields()
            this.selectOption=[]
            this.addCateForm.cat_pid = 0
            this.addCateForm.cat_level = 0
        }
    }
}
</script>

<style>
.el-breadcrumb{
    margin-bottom: 15px;
}
.zk-table--border{
    margin-top: 15px;
}
.el-cascader{
    width: 100%;
}
</style>